<?php
namespace app\admin\controller;

use app\admin\model\NewsModel;
use app\admin\model\SceneryModel;
use think\Db;
use think\Exception;
use think\Request;

class Scenery extends Base
{
    /**
     * @param Request $request
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lists (Request $request)
    {
        $sceneryModel = new SceneryModel();
        $map = [];
        $type = $request->get ('type');
        $list = $sceneryModel->getList ($map, $request->get ('page'), 10);
        return view ('lists', [
            'list' => $list,
        ]);
    }

    /**
     * @param Request $request
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail (Request $request)
    {
        $sceneryModel = new SceneryModel();
        if ($request->isPost ()) {
            try {
                Db::startTrans ();
                $newsModel = new NewsModel();
                //修改逻辑
                $id = $request->post ('id');
                $introduceId = $request->post ('introduce_id');
                $introduceDetail = [
                    'title'       => $request->post ('name') . '-简介',
                    'author'      => '溪边居',
                    'description' => $request->post ('description'),
                    'content'     => $request->post ('introduce'),
                    'status'      => $request->post ('status'),
                    'type'        => '4',
                    'updated_at'  => time ()
                ];
                if (empty($introduceId)) {
                    $introduceDetail['created_at'] = $introduceDetail['updated_at'];
                    $introduceDetail = $newsModel->create ($introduceDetail);
                    $introduceId = $introduceDetail->id;
                } else {
                    $newsModel->where (['id' => $introduceId])->update ($introduceDetail);
                }
                if (empty($introduceId)) throw new Exception('简介保存失败');
                $trafficId = $request->post ('traffic_id');
                $trafficDetail = [
                    'title'       => $request->post ('name') . '-交通方式',
                    'author'      => '溪边居',
                    'description' => $request->post ('description'),
                    'content'     => $request->post ('traffic'),
                    'status'      => $request->post ('status'),
                    'type'        => '4',
                    'updated_at'  => time ()
                ];
                if (empty($trafficId)) {
                    $trafficDetail['created_at'] = $trafficDetail['updated_at'];
                    $trafficDetail = $newsModel->create ($trafficDetail);
                    $trafficId = $trafficDetail->id;
                } else {
                    $newsModel->where (['id' => $trafficId])->update ($trafficDetail);
                }
                if (empty($introduceId)) throw new Exception('交通方式保存失败');
                $detail = [
                    'id'                => $id,
                    'name'              => $request->post ('name'),
                    'pic_id'            => $request->post ('pic_id'),
                    'status'            => $request->post ('status'),
                    'address_longitude' => $request->post ('address_longitude'),
                    'address_latitude'  => $request->post ('address_latitude'),
                    'introduce_id'      => $introduceId,
                    'traffic_id'        => $trafficId,
                    'updated_at'        => time ()
                ];
                if (empty($id)) {
                    $detail['created_at'] = $detail['updated_at'];
                    $sceneryModel->create ($detail);
                } else {
                    $sceneryModel->where (['id' => $id])->update ($detail);
                }
                Db::commit ();
                $this->ajaxMsg ('SUCCESS');
            } catch (Exception $e) {
                Db::rollback ();
                $this->ajaxMsg ($e->getMessage (), '500');
            }
        } else {
            //修改逻辑
            $id = $request->get ('id');
            if (empty($id)) {
                $detail = ['id' => '', 'name' => '', 'pic' => '', 'pic_url' => '', 'status' => '', 'address_longitude' => '', 'address_latitude' => '', 'introduce' => '', 'introduce_id' => '', 'traffic' => '', 'traffic_id' => ''];
            } else {
                $sceneryModel = new SceneryModel();
                $detail = $sceneryModel->getDetails (['s.id' => $id]);
            }
            return view ('detail', [
                'detail' => $detail,
            ]);
        }
    }

    /**
     * @param Request $request
     * @return \think\response\View
     */
    public function newsList (Request $request)
    {
        $sceneryModel = new SceneryModel();
        $map = [];
        $map['s.scenery_id'] = $request->get ('id');
        $list = $sceneryModel->getNewsList ($map, $request->get ('page'), 10);
        return view ('news_list', [
            'list' => $list,
        ]);
    }
}
